package view.macrogui
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author simon
	 */
	public class ToggleButton extends Sprite
	{
		static private const BG_NORMAL:Number = 0x555555;
		static private const BG_HOVER:Number = 0x666666;
		private var w:int;
		private var h:int;
		private var open_arrow:Shape;
		private var close_arrow:Shape;
		
		public function ToggleButton(w:int, h:int)
		{
			super();
			this.h = h;
			this.w = w;
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			open_arrow = new Shape();
			open_arrow.graphics.lineStyle(1, 0xCCCCCC);
			open_arrow.graphics.moveTo(3, (h / 2) - 12);
			open_arrow.graphics.lineTo(w - 3, (h / 2));
			open_arrow.graphics.lineTo(3, (h / 2) + 12);
			addChild(open_arrow);
			
			close_arrow = new Shape();
			close_arrow.graphics.lineStyle(1, 0xCCCCCC);
			close_arrow.graphics.moveTo(w - 3, (h / 2) - 12);
			close_arrow.graphics.lineTo(3, (h / 2));
			close_arrow.graphics.lineTo(w - 3, (h / 2) + 12);
			close_arrow.visible = false;
			addChild(close_arrow);
			
			bg = BG_NORMAL;
			
			y = (stage.stageHeight - 30) / 2 - 32;
			
			addEventListener(MouseEvent.MOUSE_OVER, function(e:Event):void
				{
					bg = BG_HOVER;
				});
			addEventListener(MouseEvent.MOUSE_OUT, function(e:Event):void
				{
					bg = BG_NORMAL;
				});
		}
		
		private function set bg(color:int):void
		{
			graphics.clear();
			graphics.beginFill(color, 0.8);
			graphics.drawRoundRectComplex(0, 0, w, h, 8, 0, 8, 0);
			graphics.endFill();
		}
		
		public function toggle():void
		{
			open_arrow.visible = !open_arrow.visible;
			close_arrow.visible = !close_arrow.visible;
		}
		
		public function open():void
		{
			open_arrow.visible = true;
			close_arrow.visible = false;
		}
		
		public function close():void
		{
			open_arrow.visible = false;
			close_arrow.visible = true;
		}
	}

}